package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class View
 */
public class View extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ServletConfig config;
	
	String page="ViewEntry.jsp";
	
	public void init(ServletConfig config)

			  throws ServletException{

			 this.config=config;

			 }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("We are in Service method of View");
		PrintWriter out=response.getWriter(); 
		
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		
		String sp=request.getParameter("choose");
		String ed=request.getParameter("EntryDate");
		
		String msg=" ";
		
		// Connection conn = null;
		 String url = "jdbc:mysql://localhost:3306/";
		 String dbName = "webappschema";
		 String driver = "com.mysql.jdbc.Driver";
		 String dbusername = "root";
		 String dbpassword = "webapp";
		 List dataList = new ArrayList(); 
		 if(sp.equals("---- Please select ----") ||  ed.equals("yyyy-mm-dd"))  
			    {
			        msg = "Please insert valid data";
			        response.setContentType("text/html");
					 
					 out.println("<font size = '6' color=red>" + msg + "</font>");
			        
			    }
		 else if(sp.equals("Purchase")){
			    		 try {
							 Class.forName(driver).newInstance();
							 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
							 String strQuery = ("SELECT Purchaseid, Grossweight, tareweight, Netweight, Date, Agency.AgencyName, RiceMill.RiceMillName FROM purchase, agency, ricemill WHERE Agencyid = Agency_Agencyid AND RiceMillid = RiceMill_RiceMillid AND Date = '" + ed +"'");
							 Statement st = conn.createStatement();
							 ResultSet rs = st.executeQuery(strQuery);
							 response.setContentType("text/html");
							 
							 while(rs.next()){
								 
								 dataList.add(rs.getInt("Purchaseid"));
								 dataList.add(rs.getInt("Grossweight"));
								 dataList.add(rs.getInt("tareweight"));
								 dataList.add(rs.getInt("Netweight"));
								 dataList.add(rs.getString("Date"));
								 dataList.add(rs.getString("AgencyName"));
								 dataList.add(rs.getString("RiceMillName"));
								 
							 }
							 
							 rs.close();
							 st.close();
						 	 } catch (Exception e) {
						 	   e.printStackTrace();
						 	 }
				    	 request.setAttribute("data", dataList);
				    	 RequestDispatcher dispatcher = request.getRequestDispatcher("ViewEntryPurchase.jsp");

				    	  if (dispatcher != null){

				    	  dispatcher.forward(request, response);
				    	//	msg = "Hi"  ;

				    	  }	    
		}

	
			    		
			    	else {
			    	 try {
						 Class.forName(driver).newInstance();
						 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
						 String strQuery = ("SELECT Saleid, Date, Weight, Price, Agency.AgencyName, RiceMill.RiceMillName, Stock.StockName FROM sale, agency, ricemill, stock WHERE Agencyid = Agency_Agencyid AND RiceMillid = RiceMill_RiceMillid AND Stockid = Stock_Stockid AND Date = '" + ed +"'");
						 Statement st = conn.createStatement();
						 ResultSet rs = st.executeQuery(strQuery);
						 response.setContentType("text/html");
						 
						 while(rs.next()){
							 
							 dataList.add(rs.getInt("Saleid"));
							 dataList.add(rs.getString("Date"));
							 dataList.add(rs.getInt("Weight"));
							 dataList.add(rs.getInt("Price"));
							 dataList.add(rs.getString("AgencyName"));
							 dataList.add(rs.getString("RiceMillName"));
							 dataList.add(rs.getString("StockName"));
							 
						 }
						 
						 
						 rs.close();
						 st.close();
					 	 } catch (Exception e) {
					 	   e.printStackTrace();
					 	 }
			    	 request.setAttribute("data", dataList);
			    	 RequestDispatcher dispatcher = request.getRequestDispatcher("ViewEntry.jsp");

			    	  if (dispatcher != null){

			    	  dispatcher.forward(request, response);
			    	//	msg = "Hi"  ;

			    	  }	    
	}
        }
		 else{  
				out.print("Please login first");    
	            request.getRequestDispatcher("index.jsp").include(request, response);  
	        } 
	        out.close();
}
}


